package lifeOfAFish.splines;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import javax.media.opengl.GL;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.glu.GLU;

import lifeOfAFish.scenegraph.sgGeometry;
import lifeOfAFish.scenegraph.sgGlobals;

import com.sun.opengl.util.BufferUtil;
import com.sun.opengl.util.GLUT;
import com.sun.opengl.util.texture.TextureIO;


public class AquariumFloor extends sgGeometry
{
  private GLCapabilities caps;
  private static final float ctrlpoints[][][] = new float[][][]
  {
  {
  { 0.0f, 0.0f, 0.0f },
  { 0.0f, 0.66f, 0.0f },
  { 0.0f, 1.32f, 0.0f },
  { 0.0f, 2.0f, 0.0f } },
  {

  { 1.16f, 0.0f, 0.0f  },
  { 1.16f, 0.66f, 0.6f  },
  { 1.16f, 1.32f, 1.0f },
  { 1.16f, 2.0f, 0.0f  } },
  {
  { 2.32f, 0.0f, 0.0f },
  { 2.32f, 0.66f, 0.6f },
  { 2.32f, 1.32f, 0.4f },
  { 2.32f, 2.0f, 0.0f } },
  {
  { 3.5f, 0.0f, 0.0f },
  { 3.5f, 0.66f, 0.0f },
  { 3.5f, 1.32f, 0.0f },
  { 3.5f, 2.0f, 0.0f } } };  
  // need float buffer instead of n-dimensional array above
  private FloatBuffer ctrlpointsBuf = BufferUtil
      .newFloatBuffer(ctrlpoints.length * ctrlpoints[0].length
                      * ctrlpoints[0][0].length);
  {
    for (int i = 0; i < ctrlpoints.length; i++)
    {
      // System.out.print(ctrlpoints.length+ " ");
      for (int j = 0; j < ctrlpoints[0].length; j++)
      {
        // System.out.println(ctrlpoints[0][0].length+" ");
        for (int k = 0; k < ctrlpoints[0][0].length; k++)
        {
          ctrlpointsBuf.put(ctrlpoints[i][j][k]);
          System.out.print(ctrlpoints[i][j][k] + " ");
        }
        System.out.println();
      }
    }
    // THEN rewind it before use
    ctrlpointsBuf.rewind();
  }


  // Texture variables
  private float[][][] texpts = new float[][][] {
	        { { 0.0f, 0.0f }, { 0.0f, 1.0f } },
	        { { 1.0f, 0.0f }, { 1.0f, 1.0f } } };
	    private FloatBuffer texptsBuf = BufferUtil
	            //
	            .newFloatBuffer(texpts.length * texpts[0].length * texpts[1].length);
	    {
	        for (int i = 0; i < texpts.length; i++)
	            // System.out.print(ctrlpoints.length+ " ");
	            for (int j = 0; j < texpts[0].length; j++) {
	            // System.out.println(ctrlpoints[0][0].length+" ");
	            for (int k = 0; k < texpts[0][0].length; k++) {
	                texptsBuf.put(texpts[i][j][k]);
	                // System.out.print(texpts[i][j][k] + " ");
	            }
	            // System.out.println(texptsBuf.toString());
	            }
	        // THEN rewind it before use
	        texptsBuf.rewind();
	        
	    };
  private static final int imageWidth = 64;
  private static final int imageHeight = 64;
  private static byte image[] = new byte[3 * imageWidth * imageHeight];
  private static ByteBuffer imageBuf = BufferUtil.newByteBuffer(image.length);
  
  
  
  
  public AquariumFloor()
  {
  
  }

  public void init()
  {

//	  sgGlobals.gl.glEnable(GL.GL_MAP2_VERTEX_3);
//	  sgGlobals.gl.glEnable(GL.GL_AUTO_NORMAL);
//	  sgGlobals.gl.glEnable(GL.GL_NORMALIZE);
//	  sgGlobals.gl.glMap2f(GL.GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, ctrlpointsBuf);
//	  sgGlobals.gl.glMapGrid2f(20, 0.0f, 1.0f, 20, 0.0f, 1.0f);
	  
	  sgGlobals.gl.glMap2f(GL.GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, ctrlpointsBuf);
	  sgGlobals.gl.glMap2f(GL.GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2, texptsBuf);
	  sgGlobals.gl.glEnable(GL.GL_MAP2_TEXTURE_COORD_2);
	  sgGlobals.gl.glEnable(GL.GL_MAP2_VERTEX_3);
	  sgGlobals.gl.glMapGrid2f(20, 0.0f, 1.0f, 20, 0.0f, 1.0f);
	  //new line 
	  
	  
//      makeImage();
//      sgGlobals.gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);
//      sgGlobals.gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
//      sgGlobals.gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
//      sgGlobals.gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
//      sgGlobals.gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
//      sgGlobals.gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, imageWidth, imageHeight, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, imageBuf);
//      sgGlobals.gl.glEnable(GL.GL_TEXTURE_2D);
//      sgGlobals.gl.glEnable(GL.GL_DEPTH_TEST);
//      sgGlobals.gl.glShadeModel(GL.GL_FLAT);

//    initlights(sgGlobals.gl);
    
  }
  
    /**
	 * Render the model, optional binding texture if necessary
	 */
	protected void draw()
	{
		GL gl = sgGlobals.gl;
//		sgGlobals.gl.glEnable(GL.GL_COLOR_MATERIAL);
		gl.glPushMatrix();
		 this.bindTexture();
		  sgGlobals.gl.glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
		  sgGlobals.gl.glEvalMesh2(GL.GL_FILL, 0, 20, 0, 20);
		  this.unbindTexture();
//		  sgGlobals.gl.glDisable(GL.GL_COLOR_MATERIAL);
		  gl.glPopMatrix();
	}

	@Override
	public float[] getBoundingBox() {
		// TODO Auto-generated method stub
		return null;
	};

}